import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'home',
        component: () =>
            import ('../views/HomeBase.vue')
    },
    {
        path: '/university',
        name: 'university',
        component: () =>
            import ('../views/SearchUniversity.vue')
    },
    {
        path: '/target',
        name: 'target',
        component: () =>
            import ('../views/TargetMy.vue')
    },
    {
        path: '/login',
        name: 'login',
        component: () =>
            import ('../views/MyLogin.vue')
    },
    {
        path: '/register',
        name: 'register',
        component: () =>
            import ('../views/MyRegister.vue')
    },
    {
        path: '/addScore',
        name: 'addScore',
        component: () =>
            import ('../views/AddScore.vue')
    },
    {
        path: '/changemy',
        name: 'changemy',
        component: () =>
            import ('../views/ChangeMy.vue')
    },
    {
        path: '/news',
        name: 'news',
        component: () =>
            import ('../views/NewsPage.vue')
    },
    {
        path: '/mysign',
        name: 'mysign',
        component: () => {
            import ('../views/MySign.vue')
        }
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    const id = window.sessionStorage.getItem('id')
        // console.log(to, from);
    if (to.name == "addScore" || to.name == "changemy") {
        if (!id) {
            window.sessionStorage.removeItem('user')
            next('/login')
            return alert("用户ID错误，请重新登录")
        } else {
            next()
        }
    }
    next()
})

export default router